{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[side:-1, price:18, side:-1, price:16, side:-1, price:15, side:-1, price:14, side:-1, price:12]\n"
     ]
    }
   ],
   "source": [
    "\"\"\"\n",
    "测试对象排序\n",
    "\"\"\"\n",
    "from typing import List\n",
    "class IncItem:\n",
    "    \"\"\"\n",
    "    加仓项\n",
    "\n",
    "    \"\"\"\n",
    "    def __init__(self, side, price):\n",
    "        self.side: int = side\n",
    "        self.opt_price: float = price\n",
    "\n",
    "    def __str__(self):\n",
    "        return f\"side:{self.side}, price:{self.opt_price}\"\n",
    "    \n",
    "    def __repr__(self) -> str:\n",
    "        return self.__str__()\n",
    "\n",
    "\n",
    "incs: List[IncItem] = []\n",
    "side = -1\n",
    "incs.append(IncItem(side, 15))\n",
    "incs.append(IncItem(side, 14))\n",
    "incs.append(IncItem(side, 18))\n",
    "incs.append(IncItem(side, 12))\n",
    "incs.append(IncItem(side, 16))\n",
    "\n",
    "incs.sort(key=lambda x: x.opt_price, reverse=side!=1)\n",
    "\n",
    "print(incs)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "per.days>0:  False\n",
      "per.seconds > 3600:  True\n"
     ]
    }
   ],
   "source": [
    "from datetime import datetime, timedelta\n",
    "dt1 = datetime.strptime(\"2023-04-03 10:55:00\", \"%Y-%m-%d %H:%M:%S\")\n",
    "dt2 = datetime.strptime(\"2023-04-04 01:56:00\", \"%Y-%m-%d %H:%M:%S\")\n",
    "per = dt2 - dt1\n",
    "print(\"per.days>0: \", per.days>0)\n",
    "print(\"per.seconds > 3600: \", per.seconds > 3600)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "None\n"
     ]
    }
   ],
   "source": [
    "import re\n",
    "\n",
    "print(re.match(r\"[a-zA-Z]+\\d{2,}\", \"ag5\"))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "signal:-10\n",
      "signal:1\n"
     ]
    }
   ],
   "source": [
    "import copy\n",
    "\n",
    "class DDSignalItem:\n",
    "    def __init__(self, signal, period=1440):\n",
    "        self.signal = signal   # 日线顶底信号\n",
    "        self.period: int = period\n",
    "\n",
    "    def __str__(self):\n",
    "        return \"signal:{}\".format(self.signal)\n",
    "\n",
    "    def __repr__(self):\n",
    "        return self.__str__()\n",
    "    \n",
    "a = DDSignalItem(1, 1440)\n",
    "b = copy.copy(a)\n",
    "a.signal = -10\n",
    "\n",
    "print(a)\n",
    "print(b)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'c': 3, 'd': 4}\n"
     ]
    }
   ],
   "source": [
    "a = {\"a\":1, \"b\":2, \"c\":3, \"d\":4}\n",
    "for key in list(a.keys()):\n",
    "    if key == \"a\" or key == \"b\" or key == \"c\":\n",
    "        del a[key]\n",
    "        continue\n",
    "\n",
    "print(a)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2023-05-29 15:00:00\n",
      "1685416500 2023-05-30 11:15:00\n",
      "1685545200 2023-05-31 23:00:00\n",
      "aaa, 1685470500 2023-05-31 02:15:00\n",
      "ddd, 1685471400 2023-05-31 02:30:00\n",
      "eee 1685625300 2023-06-01 21:15:00\n"
     ]
    }
   ],
   "source": [
    "from datetime import datetime\n",
    "print(datetime.fromtimestamp(1685343600))\n",
    "print(\"1685416500\", datetime.fromtimestamp(1685416500))\n",
    "print(\"1685545200\", datetime.fromtimestamp(1685545200))\n",
    "print(\"aaa, 1685470500\", datetime.fromtimestamp(1685470500))\n",
    "print(\"ddd, 1685471400\", datetime.fromtimestamp(1685471400))\n",
    "print(\"eee\", 1685625300, datetime.fromtimestamp(1685625300))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "datetime.timedelta(days=182)"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from datetime import datetime\n",
    "t2 = datetime.strptime('2023-06-01', '%Y-%m-%d')\n",
    "t1 = datetime.strptime('2022-12-01', '%Y-%m-%d')\n",
    "\n",
    "t2 - t1\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1764342952336\n",
      "1764342200352\n",
      "1 True\n"
     ]
    }
   ],
   "source": [
    "import copy\n",
    "class Hold():\n",
    "    def __init__(self, id, dir, posi) -> None:\n",
    "        self.InstrumentID = id\n",
    "        self.Direction = dir\n",
    "        self.Position = posi\n",
    "    def __str__(self) -> str:\n",
    "        return f\"{self.InstrumentID},{self.Direction}, {self.Position}\"\n",
    "    def __repr__(self) -> str:\n",
    "        return self.__str__()\n",
    "    def __eq__(self, other):\n",
    "        if isinstance(other, Hold):\n",
    "            return self.InstrumentID == other.InstrumentID and self.Direction == other.Direction and self.Position == other.Position\n",
    "        return False\n",
    "h1 = {}\n",
    "h1[\"ag2312\"] = Hold(\"ag2312\", \"2\", 15)\n",
    "h1[\"au2310\"] = Hold(\"au2310\", \"2\", 14)\n",
    "print(id(h1[\"ag2312\"]))\n",
    "h2 = copy.deepcopy(h1)\n",
    "print(id(h2[\"ag2312\"]))\n",
    "print(\"1\", h1 == h2)\n",
    "h2[\"ag2312\"].Position = 10\n",
    "\n",
    "\n",
    "class Order:\n",
    "    def __init__(self, id, direction, volume, type=0) -> None:\n",
    "        self.instrument_id = id\n",
    "        self.posi_direction = direction\n",
    "        self.volume = volume\n",
    "        self.type = type\n",
    "        \n",
    "orders = []\n",
    "open_o = Order(\"ag2305\", \"2\", 15)\n",
    "orders.append(open_o)\n",
    "o = Order(\"ag2305\", \"2\", 3, 1)\n",
    "for o1 in orders:\n",
    "    v = min(o1.volume, o.volume)\n",
    "    o.volume -= v\n",
    "    o1.volume -=v\n",
    "    orders\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['tbl_code_ag_all']"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import re\n",
    "pattern = r'^tbl_code_.+_all$'\n",
    "re.findall(pattern, \"tbl_code_ag_all\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "34.66725820763088"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "39.07/1.127\n",
    "\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "pydev3.9",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.16"
  },
  "orig_nbformat": 4
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
